<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>付款编辑</title>
	<!-- import css and js --> 
	<%@ include file="/common/metaInput.jsp" %>  
	<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
	<script type="text/javascript" src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="${ctx}/js/jsp/yd/logic/ItemStateData.js"></script>
<script type="text/javascript">
var basic_Url="${ctx}/yd/logic/fk";
var ItemStateList = ItemStateData.Rows;
var grid;
var tax_price=null; 
$(function(){
	var column1 = [
				      { display: 'id', name:'id', width: 1 ,hide:true},
					  { display: '付款单id', name:'fkid',  width: 1 ,hide:true},
					  { display: '付款单编号', name:'fkcode', width: 1,hide:true},
	//				  { display: '<font color="gray">批次</font>', name:'batch', width: 120}, 
					  { display: '<font color="gray">物料编码</font>', name:'code', width: 120},
					  { display: '<font color="gray">物料名称</font>', name:'name', width: 120},
					  /* { display: '<font color="gray">等级</font>', name:'status', width: 100, render: function (item)
	                      {
	                      for (var i = 0; i < ItemStateList.length; i++)
	                      {
	                          if (ItemStateList[i]['ItemStateID'] == item.status){
	                        	  return ItemStateList[i]['ItemStateName'];
	                          }
	                      }
	                      return item.status;
	                  }}, */
	                  { display: '<font color="gray">规格</font>', name: 'spec', width: 270},
	                  { display: '<font color="gray">单位</font>', name: 'unit', width: 120},
	                  { display: '<font color="gray">数量</font>', name: 'quantity', width: 100},
	                  { display: '<font color="gray">单价(含税)</font>', name: 'tax_price', width: 100}, 
	                  { display: '<font color="gray">总货款(含税)</font>', name: 'amount', width: 100,type: 'float',render: function(item){
	                	  if(item.amount == null || item.amount == "" || parseFloat(item.amount) == 0){
	                		  return "0.0";
	                	  }else{
	                		  return (item.amount*1).toFixed(4);
	                	  }
	                  }},
	                  { display: '<font color="gray">已付货款</font>', name: 'yfhk', width: 100,type: 'float',render:function(item){
	                	  if(item.yfhk == null || item.yfhk == "" || parseFloat(item.yfhk) == 0){
	                		  return "0.0";
	                	  }else{
	                		  return (item.yfhk*1).toFixed(4);
	                	  }
	          		  }},
	                  { display: '本次付款金额<font color="red">*</font>', name: 'sfhk', width: 100,type: 'float',editor: { type: 'float'},render: function(item){
	                	  if(item.sfhk == null || item.sfhk == "" || parseFloat(item.sfhk) == 0){
	                		  return "0.0";
	                	  }else{
	                		  return (item.sfhk*1).toFixed(4);
	                	  }
	                  }},
	                  { display: '<font color="gray">未付货款</font>', name: 'wfhk', width: 100,type: 'float',render: function(item){
	                	  if(item.wfhk == null || item.wfhk == "" || parseFloat(item.wfhk) == 0){
	                		  return "0.0";
	                	  }else{
	                		  return (item.wfhk*1).toFixed(4);
	                	  }
	                  }}
	                  ];
	var column2 = [
				      { display: 'id', name:'id', width: 1 ,hide:true},
					  { display: '付款单id', name:'fkid',  width: 1 ,hide:true},
					  { display: '付款单编号', name:'fkcode', width: 1,hide:true},
	//				  { display: '<font color="gray">批次</font>', name:'batch', width:120}, 
					  { display: '<font color="gray">物料编码</font>', name:'code', width: 120},
					  { display: '<font color="gray">物料名称</font>', name:'name', width: 120},
					  /* { display: '<font color="gray">等级</font>', name:'status', width: 100, render: function (item)
	                      {
	                      for (var i = 0; i < ItemStateList.length; i++)
	                      {
	                          if (ItemStateList[i]['ItemStateID'] == item.status){
	                        	  return ItemStateList[i]['ItemStateName'];
	                          }
	                      }
	                      return item.status;
	                  }}, */
	                  { display: '<font color="gray">规格</font>', name: 'spec', width: 270},
	                  { display: '<font color="gray">单位</font>', name: 'unit', width: 120},
	                  { display: '<font color="gray">数量</font>', name: 'quantity', width: 100},
	                  { display: '<font color="gray">单价(含税)</font>', name: 'tax_price', width: 100}, 
	                  { display: '<font color="gray">总货款(含税)</font>', name: 'amount', width: 100},
	                  { display: '<font color="gray">已付货款</font>', name: 'yfhk', width: 100},
	                  { display: '本次付款金额<font color="red">*</font>', name: 'sfhk', width: 100},
	                  { display: '<font color="gray">未付货款</font>', name: 'wfhk', width: 100}
	                  ];
	var column3 = null;
	if ('${xx}' == 1){
		column3 = column1;
	}else {
		column3 = column2;
	}
	grid = $("#table1").ligerGrid({
        columns: column3,
        enabledEdit: true,
        usePager: true,pageSize:1000,pageSizeOptions:[1000],
        parms:[{ name : 'id' , value : '${fk.id}'}],
      	url : basic_Url+"/griditems.do",
        width: "100%", height:235, checkbox: true,
        toolbar: { items: [
                           { text: '删除', click: deleteRow, img: '${ctx}/js/ligerUI/skins/icons/delete.gif' }
                           ]
                           }
    });
	$("#pageloading").hide();

	$("#inputForm").validate({			   
		onsubmit: true,
		 /*执行ajaxsubmit  */ 
	    submitHandler: function(editform) {
	        try{			    		
	            url =  basic_Url+"/save.do";
	            var api = frameElement.api;
	        	var W = api.opener;
	            getReturnMessage(url,"inputForm",W.searchAP);
	            W.pid='${pid}';
	        }catch(e){alert(e);}
	    }
	});
	
	if("${flag}" == "ADD"){ //新增单据
		 $("#commit").hide();//禁用提交按钮
		 $("#btn_exportExcel").hide();//禁用导出按钮
	}
	if("${flag}" == "0"){ //草稿单据
		$("#save").hide();//禁用保存按钮
		$("#btn_exportExcel").hide();//禁用导出按钮
		if($("#createName").val()!="${user.name}"){
			$("#commit").hide();
			$("input[type!='hidden']").attr("disabled","disabled");
			$("select").attr("disabled","disabled");//禁用SELECT标签
			$("textarea").attr("disabled","disabled");//禁用TEXTAREA标签
		}
	}
	if("${flag}" == "2"){ //完结单据
		$("#save").hide();//禁用保存按钮
		$("#commit").hide();//禁用提交按钮
		$("input[type!='hidden']").attr("disabled","disabled");
		$("select").attr("disabled","disabled");//禁用SELECT标签
		$("textarea").attr("disabled","disabled");//禁用TEXTAREA标签
	}

	$("#apply").change(function(){ 
		//$("#powareid").val($("#apply").val());	
		$("#oriCode").val($("#apply").find("option:selected").text());
		<c:forEach items='${powarelist}' var='state'>
	    	if($("#apply").find("option:selected").text()=='${state.powarecode}'){
	    		$("#pocode").val('${state.pocode}');
	    		$("#spCode").val('${state.spcode}');
	    		$("#spName").val('${state.spname}'); 
	    	}
   		</c:forEach>
		var path = basic_Url+"/fkitemlist.do?id="+$("#apply").val()+"&oricode="+$("#apply").find("option:selected").text();		
		multipleSearch("#table1",true,"",encodeURI(path));
		$.ajax({
			url: basic_Url+"/fklist.do",
	  	      global: false,
	  	      type: "POST",
	  	      dataType: "json",
	  	      data: ({
	  	    	id : $("#apply").val(),
	  	    	oricode: $("#apply").find("option:selected").text(),
	  	    	pocode: $("#pocode").val()
	  	      }),		     
	  	      success: function(msg){	 
		    		  $("#currency").val(msg.currency);
		    		  $("#exchangeRate").val(msg.exchangeRate);
		    		  $("#yfhk").val(msg.yfhk);
		    		  $("#amount").val(msg.amount);
	  	      }
		});
	}); 
	
	/* <c:forEach items='${fkitemlist}' var='item'>
	var manager = $("#table1").ligerGetGridManager();
	manager.addRow2({
		id: "${item.id}",
		fkid: "${item.fkid}",
		fkcode: "${item.fkcode}",
	//	batch: "${item.batch}",
		code: "${item.code}",
		name: "${item.name}",
	//	status: "${item.status}",
		unit: "${item.unit}",
		spec: "${item.spec}",
		quantity : "${item.quantity}",
		tax_price : "${item.tax_price}",
		amount : "${item.amount}",
		yfhk : "${item.yfhk}",
		sfhk : "${item.sfhk}",
		wfhk : "${item.wfhk}"
    });
	</c:forEach> */

});

function submitForm(param){
    //设置ajax全局同步
 	
	var data = grid.getData();
	var dl=grid.getDeleted();
	if(data.length==0){
		$.dialog.alert("请选择付款内容");
   	 	return false;
	}
	var ba=true;
	if(data !=null){
		var i=0;
		var sumsfhk=0;
		$(data).each(function (){
			i++;
			sumsfhk += parseFloat(this.sfhk);
			if(this.yfhk == null || this.yfhk.length<=0){
				this.yfhk = 0;
			}
			if((parseFloat(this.sfhk))>parseFloat(this.wfhk)){
				$.dialog.alert("您第" + i + "行的本次付款金额大于未付款金额");
				ba = false;
				return false;
			} 
			if(this.sfhk<=0){
				$.dialog.alert("您第" + i + "行的付款金额必须大于0");
				ba = false;
				return false;
			}
		});
		if(sumsfhk<=0){
			$.dialog.alert("您的本次付款金额不合法，请重新填写");
			ba = false;
			return false;
		}
	}
	jQuery.ajaxSettings.async = false;
	if(ba){
		$("#itemdata").val(JSON.stringify(data));
		$("#itemdelete").val(JSON.stringify(dl));
	  	$("#submitFlag").val(param);
	    $("#inputForm").submit();
	}
 }		

function tableSubmit(msg,param){
	$.ligerDialog.confirm(msg, 
			function (flag) { 
			if(flag){
				submitForm(param);
				}
		}); 
}

function submitPay(param){
	tableSubmit('是否确认提交付款？',param);
}

function deleteRow()
{ 
	grid.deleteSelectedRow();
}

function exportExcel() {
	var id =$("#id").val();
	if(id!=""){
		var path = basic_Url + "/exportExcel.do?id="+id;
		window.open(path, "_blank");
	}else{
		$.dialog.alert("获取数据出现未知问题，请重试");
	}
}
</script>

</head>

<body>

<form id="inputForm" action="" method="post">
<input type="hidden" id="id" name="id" value="${fk.id}"/>
<input type="hidden" id="itemdata" name="itemdata" value=""/>
<input type="hidden" id="itemdelete" name="itemdelete" value=""/>
<!-- <input type="hidden" id="powareid" name="powareid" value=""/> -->
<!-- 用于标识提交按钮 -->
<input type="hidden" id="submitFlag" name="submitFlag" value="" />
 <div class="pageContent" >
    <div class="panel">
    <table class="TableBlock" align="center">
    	<c:if test="${!empty fk.fkCode}">
    	<tr >
	        <td nowrap="nowrap">付款单号:</td>
	        <td >
	        	<input type="text" id="fkCode" name="fkCode" class="inp_width_98p required" readonly="readonly"  value="${fk.fkCode}" />
	        </td>
	        <td nowrap="nowrap" width="20%">建单时间:</td>
			<td width="30%"><input class="inp_width_98p" type="text" id="dctime" name="dctime"
			value="<fmt:formatDate value="${empty fk.dctime?nowDate:fk.dctime}" type="both" dateStyle="long" pattern="yyyy-MM-dd" />" readonly="readonly"/></td>
  		</tr>
  		</c:if>
	    <tr>
    		<td nowrap="nowrap">采购入库单号:</td>
    		<c:choose>
    		<c:when test="${empty fk.oriCode}">
	    		<td>
	    			<select id="apply" name="apply" class="inp_width_98p required" >
	                 	<option></option>
	                 	<c:forEach items='${powarelist}' var='state'>
	                 		<option id="${ state.powarecode}" value='${state.id}'>${state.powarecode}</option>
	                 	</c:forEach>
	                </select> 
	                <input type="hidden" id="pocode" name="pocode"  value=""/>
	                <input type="hidden" id="oriCode" name="oriCode"  value="" />	                	
	                 <script type="text/javascript">
	               	  $("#"+'${fk.oriCode}').attr("selected","selected");              	
	                </script>   
	            </td>
            </c:when>
            <c:otherwise>
            	<td> <input type="text" id="oriCode" name="oriCode" value="${fk.oriCode}" class="inp_width_98p" readonly="readonly"/></td>
           	</c:otherwise>
            </c:choose>  
            <td nowrap="nowrap">付款日期:</td>
            <c:choose>
            <c:when test="${empty fk.fkDate or flag eq 0}">
            	<td><input type="text" id="fkDate" name="fkDate" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" class="inp_width_98p Wdate required" value="<fmt:formatDate value="${fk.fkDate}" pattern="yyyy-MM-dd" type="both" />" /></td>
        	</c:when>
        	<c:otherwise>
        		<td><input type="text" id="fkDate" name="fkDate"  class="inp_width_98p Wdate" value="<fmt:formatDate value="${fk.fkDate}" pattern="yyyy-MM-dd" type="both" />" readonly="readonly" /></td>
        	</c:otherwise>
        	</c:choose>     
   		 </tr>
         <tr>
	     	<td nowrap="nowrap">供应商名称：</td>
	     	<td><input type="text" id="spName" name="spName" value="${fk.spName}" class="inp_width_98p" readonly="readonly" /></td>
         	<td nowrap="nowrap">供应商编码:</td>
         	<td><input id="spCode" name="spCode" value="${fk.spCode}" class="inp_width_98p" readonly="readonly"/>
         	<input type="hidden" id="pid" name="pid"  value="${pid}" /></td> 
	     </tr>
	     <tr>
	     	<td nowrap="nowrap">付款方式:</td>
	     	<td>
	     		<c:choose>
          		<c:when test="${empty fk.fkMethod or flag eq 0}">
          			<select id="fkMethod" name="fkMethod" class="inp_width_98p required" >
                		<option></option>
                		<c:forEach items="${payment}" var="item">
                			<option value="${item.name}">${item.name}</option>
                		</c:forEach>
                	</select>
                	<script type="text/javascript">
          				$("#fkMethod").val("${fk.fkMethod}");
          			</script>
          		</c:when>
				<c:otherwise>
					<input type="text" value="${fk.fkMethod}" id="fkMethod" name="fkMethod" class="inp_width_98p" readonly="readonly" ></input>
				</c:otherwise>
				</c:choose>
		     </td>
	     	<td nowrap="nowrap">币别：</td>
            <td>
            	<input type="text" value="${fk.currency}" id="currency" name="currency" class="inp_width_98p" readonly="readonly"></input>
		     </td>     	 	                    
	     </tr>
	     <tr>  		                 
         	<td nowrap="nowrap">付款账号：</td>         	
            <td>
            <c:choose>
            <c:when test="${empty fk.fkAccount or flag eq 0}"> 
            	<input type="text" name="fkAccount" id="fkAccount"  value="${fk.fkAccount}" class="inp_width_98p required" />
            </c:when>
            <c:otherwise>	
              <input type="text" name="fkAccount" id="skzh"  value="${fk.fkAccount}" class="inp_width_98p" readonly="readonly" />
            </c:otherwise>
            </c:choose>
            </td>
	     	<td nowrap="nowrap">汇率:</td>
	     	<td><input type="text" id="exchangeRate" name="exchangeRate" value="${fk.exchangeRate}" class="inp_width_98p" readonly="readonly" /></td>
	     </tr>
         <tr>
	        <td nowrap="nowrap">应付总货款:</td>
	        <td >
	        	<input type="text" id="amount" name="amount" class="inp_width_98p" readonly="readonly" value="${fk.amount}" />
	        </td>
	        <td nowrap="nowrap">已付货款:</td>
	        <td >
	        	<input type="text" id="yfhk" name="yfhk" class="inp_width_98p" readonly="readonly"  value="${fk.yfhk}" />
	        	<c:if test="${fk.yfhk == null}">
					<script>
						$("#yfhk").val("0");
					</script>
				</c:if>
	        </td>
  		</tr>
		<tr>
			<td nowrap="nowrap">制单人:</td>
			<td >
			    <input type="text" id="createName" name="createName" class="inp_width_98p" value="${user.name}" readonly="readonly"/>
				<c:if test="${fk.createName != null}">
					<script>
						$("#createName").val("${fk.createName}");
					</script>
				</c:if>
		  </td>
		</tr>
         <tr>
         	<td nowrap="nowrap">备注:</td>
         	<td colspan="3"><input id="remark" type="text" class="inp_width_98p" name="remark" value="${fk.remark}" /></td>
         </tr>
     </table>
     <div id="table1"> </div>               
     </div>
    <div class="formBar">
	        <ul>
		       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save" onclick="submitForm(0)">保存草稿</button></div></div></li>
		       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="commit" onclick="submitPay(1)">提交付款</button></div></div></li>
			   <li><div class="button"><div class="buttonContent"><button type="button" id="btn_exportExcel" class="export" onclick="exportExcel()">导出</button></div></div> </li>
		       <li><div class="button"><div class="buttonContent"><button type="button" class="print" onclick="CloseDialog()">关闭</button></div></div></li>
	        </ul>
        </div>    
    </div>
</form>
</body>
</html>
